/*
 * DelegateHFile.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

import com.thoughtworks.qdox.model.JavaClass;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileFooter;
import com.cute4j.tools.ng.NativeFileHeader;
import com.cute4j.tools.ng.NativeFileParts;
import com.cute4j.tools.ng.NativeFileText;
import com.cute4j.tools.ng.NativeGenerator;
import com.cute4j.tools.ng.Utils;

/**
 * Delegate Header File.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class DelegateHFile extends AbstractNativeFile {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new DelegateHFile object.
   *
   * @param delegateFolder the delegate folder
   * @param javaClass the qdox java class
   */
  public DelegateHFile(File delegateFolder, JavaClass javaClass) {
    super(delegateFolder, Utils.getDelegateName(javaClass) + ".h", javaClass, true);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected NativeFileParts createParts() {
    NativeFileParts parts = new NativeFileParts();
    parts.add(new NativeFileHeader(this));
    parts.add(new DelegateHFileStart(this, getJavaClass()));
    parts.add(new DelegateClassDeclaration(this, getJavaClass()));
    parts.add(new DelegateHFileEnd(this, getJavaClass()));
    parts.add(new NativeFileFooter(this));
    return parts;
  }

  /**
   * {@inheritDoc}
   */
  protected NativeFileParts loadParts(BufferedReader br) throws IOException {
    NativeFileParts parts = new NativeFileParts();
    String newLine = NativeGenerator.getInstance().getNewLine();
    StringBuffer sb = new StringBuffer(80);
    String line;
    int state = 0;
    while ((line = br.readLine()) != null) {
      if (line.endsWith("// native calls")) {
        switch (state) {
          case 0:
            parts.add(new NativeFileText(this, sb.toString()));
            break;
          case 1:
            parts.add(new NativeCallsDeclaration(this, sb.toString(),
                                                 getJavaClass()));
            break;
          case 2:
            parts.add(new NativeCallbacksDeclaration(this, sb.toString(),
                                                     getJavaClass()));
            break;
        }
        state = 1;
        sb.setLength(0);
      } else if (line.endsWith("// native callbacks")) {
        switch (state) {
          case 0:
            parts.add(new NativeFileText(this, sb.toString()));
            break;
          case 1:
            parts.add(new NativeCallsDeclaration(this, sb.toString(),
                                                 getJavaClass()));
            break;
          case 2:
            parts.add(new NativeCallbacksDeclaration(this, sb.toString(),
                                                     getJavaClass()));
            break;
        }
        state = 2;
        sb.setLength(0);
      } else if ((line.length() > 0) && (line.charAt(0) != ' ') &&
                 (line.charAt(0) != '/')) {
        switch (state) {
          case 1:
            parts.add(new NativeCallsDeclaration(this, sb.toString(),
                                                 getJavaClass()));
            state = 0;
            sb.setLength(0);
            break;
          case 2:
            parts.add(new NativeCallbacksDeclaration(this, sb.toString(),
                                                     getJavaClass()));
            state = 0;
            sb.setLength(0);
            break;
        }
      }
      sb.append(line);
      sb.append(newLine);
    }
    parts.add(new NativeFileText(this, sb.toString()));
    return parts;
  }

}
